#include<iostream>
#include<unistd.h>
#include<stdio.h>
#include<poll.h>
#include<string.h>
#include"log.hpp"
#include"errno.hpp"

int main()
{ 
    //使用poll监视，标准输入里有数据是可读的
    struct pollfd fds;
    bzero(&fds,sizeof(fds));
    fds.fd  = 0;
    fds.events = POLLIN; //表示关心读事件
    
    for( ; ; )
    {
        int n = poll(&fds, 1 , 1000);
        if(n < 0)
        {
            logMessage(FATAL);
            exit(POLL_ERRON);
        }
        else if(n == 0)
        {
            std::cout << "标准输入里什么都没有，不可以读" << std::endl;
        } 
        else if(n > 0)
        {
             //表示就绪
             if(fds.revents == POLLIN)
             {
                 char buffer[1024];
                 ssize_t s = read(fds.fd,buffer,sizeof buffer - 1);

                 if(s < 0)
                 {
                    logMessage(FATAL);
                    exit(READ_ERRON);
                 }

                 buffer[s-1] = 0;
                 std::cout << buffer << std::endl;
             }
        }
    }
}
